﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace QRCodeTools {
    public partial class ScreenCaptureForm : Form {
        public ScreenCaptureForm() {
            InitializeComponent();
        }

        protected MouseHook MouseHook = new MouseHook();
        protected Bitmap CurrScreenImage { get; set; }

        protected ScreenSize PreMouseInScreen = null;


        public event EventHandler OnEndCapture;

        public event Action<Bitmap> OnScreenCapture;


        protected override void OnLostFocus(EventArgs e) {
            base.OnLostFocus(e);
        }
        protected override void OnDeactivate(EventArgs e) {
            base.OnDeactivate(e);
            EndCapture();
        }

        private void ScreenCaptureForm_KeyDown(object sender, KeyEventArgs e) {

        }

        public new void Show() {
            base.Show();
            MouseHook.Start();
            //其实这里不准确.MousePosition只是单屏内的坐标,并不是虚拟屏幕的坐标
            //所以导致下面的效果是首次打开都在主屏显示截图界面
            ResetScreenCaptureByMousePos(MousePosition);
        }

        private void ScreenCaptureForm_Load(object sender, EventArgs e) {
            MouseHook.OnMouseActivity += MouseHook_OnMouseActivity;
        }

        private void MouseHook_OnMouseActivity(object sender, MouseEventArgs e) {
            //if (e.Button != MouseButtons.Right) return;//用于测试
            if (e.Button != MouseButtons.None || e.Delta != 0) return;
            ResetScreenCaptureByMousePos(e.Location);
        }

        protected void ResetScreenCaptureByMousePos(Point mousePos) {
            if (Disposing || IsDisposed) return;
            var mouseInScreen = ScreenHelper.PosInScreen(mousePos);
            if (mouseInScreen == null) return;
            if (PreMouseInScreen == null || PreMouseInScreen.DeviceName != mouseInScreen.DeviceName) {
                ClearCapture();
                PreMouseInScreen = mouseInScreen;
                Opacity = 0;
                PerformLayout();

                SuspendLayout();
                Left = mouseInScreen.RawBounds.X;
                Top = mouseInScreen.RawBounds.Y;
                Width = mouseInScreen.RawBounds.Width;
                Height = mouseInScreen.RawBounds.Height;
                CurrScreenImage = new Bitmap(mouseInScreen.RawBounds.Width, mouseInScreen.RawBounds.Height);
                using (Graphics g = Graphics.FromImage(CurrScreenImage)) {
                    g.Clear(Color.Black);
                    g.CopyFromScreen(mouseInScreen.RawBounds.Location, Point.Empty, CurrScreenImage.Size);
                }
                OnScreenCapture?.Invoke(CurrScreenImage);
                ScreenCaptureImage.Image = CurrScreenImage.CompressImage(Width, Height);
                ResumeLayout();

                //因为控件的图片刷新不是同步的,所以需要延时一下显示窗体,否则窗体会闪烁()
                Task.Delay(100).ContinueWith((t) => {
                    if (Disposing || IsDisposed) return;
                    Invoke(new Action(() => {
                        Opacity = 1;
                    }));
                });
            }
        }


        private void ScreenCaptureForm_FormClosed(object sender, FormClosedEventArgs e) {
        }

        private void ScreenCaptureForm_FormClosing(object sender, FormClosingEventArgs e) {
            e.Cancel = true;
            EndCapture();
        }

        private void ClearCapture() {
            SelectPanel.Controls.Clear();
        }

        /// <summary>
        /// 停止捕获
        /// </summary>
        public void EndCapture(bool triggerEvent = true) {
            ClearCapture();
            PreMouseInScreen = null;
            MouseHook.Stop();
            Hide();
            if (triggerEvent) {
                OnEndCapture?.Invoke(this, EventArgs.Empty);
            }
        }

        private void BtnCancel_Click(object sender, EventArgs e) {
            EndCapture();
        }
    }
}
